package com.booter.webflux.config;

import com.booter.webflux.service.MenuFluxHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class RouteFunctionConfig {

    @Autowired
    private MenuFluxHandler handler;

    @Bean
    public RouterFunction<ServerResponse> menuRouter() {
        //级联路由
        return
                //设置path 类似controller类上的RequestMapping
                RouterFunctions.nest(RequestPredicates.path("/route"),

                        RouterFunctions.route(RequestPredicates.path("/menu/list"), this.handler::getMenuList)
                                //第二个路由
                                .andRoute(RequestPredicates.POST("/menu/save")
                                        .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), this.handler::save)
                                //第三个路由
                                .andRoute(RequestPredicates.GET("/menu/delete/{id}"), this.handler::delete));


    }

}
